from django.test import TestCase
from django.contrib.auth.models import User
from rest_framework.test import APITestCase
from rest_framework import status
from .models import UserChatRoom, UserChatMessage


class UserChatRoomModelTest(TestCase):
    """聊天室模型测试"""
    
    def setUp(self):
        self.user1 = User.objects.create_user(username='user1', password='pass123')
        self.user2 = User.objects.create_user(username='user2', password='pass123')
        
    def test_create_private_chat_room(self):
        """测试创建一对一聊天室"""
        room = UserChatRoom.objects.create(room_type='private')
        room.participants.add(self.user1, self.user2)
        
        self.assertEqual(room.room_type, 'private')
        self.assertEqual(room.participants.count(), 2)
        self.assertIn(self.user1, room.participants.all())
        self.assertIn(self.user2, room.participants.all())
    
    def test_get_other_participant(self):
        """测试获取对话中的另一个参与者"""
        room = UserChatRoom.objects.create(room_type='private')
        room.participants.add(self.user1, self.user2)
        
        other_user = room.get_other_participant(self.user1)
        self.assertEqual(other_user, self.user2)
        
        other_user = room.get_other_participant(self.user2)
        self.assertEqual(other_user, self.user1)


class UserChatMessageModelTest(TestCase):
    """聊天消息模型测试"""
    
    def setUp(self):
        self.user1 = User.objects.create_user(username='user1', password='pass123')
        self.user2 = User.objects.create_user(username='user2', password='pass123')
        self.room = UserChatRoom.objects.create(room_type='private')
        self.room.participants.add(self.user1, self.user2)
    
    def test_create_text_message(self):
        """测试创建文本消息"""
        message = UserChatMessage.objects.create(
            room=self.room,
            sender=self.user1,
            content='Hello, how are you?',
            message_type='text'
        )
        
        self.assertEqual(message.room, self.room)
        self.assertEqual(message.sender, self.user1)
        self.assertEqual(message.content, 'Hello, how are you?')
        self.assertEqual(message.message_type, 'text')


class ChatAPITest(APITestCase):
    """聊天API测试"""
    
    def setUp(self):
        self.user1 = User.objects.create_user(username='user1', password='pass123')
        self.user2 = User.objects.create_user(username='user2', password='pass123')
    
    def test_create_private_chat_room(self):
        """测试通过API创建一对一聊天室"""
        url = '/company_chat/api/rooms/create_private_chat/'
        data = {'other_user_id': self.user2.id}
        
        response = self.client.post(url, data, format='json')
        
        # 由于没有认证，可能返回不同状态码，这里主要测试API结构
        self.assertIn(response.status_code, [200, 201, 401, 403])
    
    def test_send_message_to_room(self):
        """测试发送消息到聊天室"""
        # 创建聊天室
        room = UserChatRoom.objects.create(room_type='private')
        room.participants.add(self.user1, self.user2)
        
        url = f'/company_chat/api/rooms/{room.id}/send_message/'
        data = {
            'content': 'Hello World!',
            'message_type': 'text'
        }
        
        response = self.client.post(url, data, format='json')
        
        # 由于没有认证，可能返回不同状态码
        self.assertIn(response.status_code, [201, 401, 403])
    
    def test_search_contacts(self):
        """测试搜索联系人"""
        url = '/company_chat/api/users/search_contacts/'
        
        response = self.client.get(url)
        
        # 测试API端点存在
        self.assertIn(response.status_code, [200, 401, 403])